package com.xpengplayer.data;

import android.content.Context;
import android.content.SharedPreferences;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * 收藏目录管理器
 * 负责管理用户收藏的视频目录
 */
public class FavoriteDirectoryManager {
    private static final String TAG = "FavoriteDirectoryManager";
    private static final String PREF_NAME = "favorite_directories";
    private static final String KEY_FAVORITE_LIST = "favorite_list";

    private SharedPreferences preferences;
    private Gson gson;

    public FavoriteDirectoryManager(Context context) {
        this.preferences = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
        this.gson = new Gson();
    }

    /**
     * 获取所有收藏目录
     * @return 按添加时间排序的收藏目录列表
     */
    public List<FavoriteDirectory> getAllFavorites() {
        String favoritesJson = preferences.getString(KEY_FAVORITE_LIST, "[]");
        Type type = new TypeToken<List<FavoriteDirectory>>(){}.getType();
        List<FavoriteDirectory> favoriteList = gson.fromJson(favoritesJson, type);

        // 按添加时间升序排列
        if (favoriteList != null) {
            Collections.sort(favoriteList, new Comparator<FavoriteDirectory>() {
                @Override
                public int compare(FavoriteDirectory f1, FavoriteDirectory f2) {
                    return Long.compare(f1.getAddTime(), f2.getAddTime());
                }
            });
        }

        return favoriteList != null ? favoriteList : new ArrayList<>();
    }

    /**
     * 添加收藏目录
     * @param directory 收藏目录对象
     * @return 如果添加成功返回true，如果目录已存在返回false
     */
    public boolean addFavorite(FavoriteDirectory directory) {
        List<FavoriteDirectory> favoriteList = getAllFavorites();

        // 检查是否已存在相同路径的目录
        for (FavoriteDirectory favDir : favoriteList) {
            if (favDir.getDirectoryPath().equals(directory.getDirectoryPath())) {
                return false;  // 目录已存在
            }
        }

        // 添加新目录
        favoriteList.add(directory);
        saveFavoriteList(favoriteList);
        return true;
    }

    /**
     * 移除收藏目录
     * @param directoryPath 要移除的目录路径
     * @return 如果移除成功返回true，如果目录不存在返回false
     */
    public boolean removeFavorite(String directoryPath) {
        List<FavoriteDirectory> favoriteList = getAllFavorites();
        for (int i = 0; i < favoriteList.size(); i++) {
            if (favoriteList.get(i).getDirectoryPath().equals(directoryPath)) {
                favoriteList.remove(i);
                saveFavoriteList(favoriteList);
                return true;
            }
        }
        return false;  // 目录不存在
    }

    /**
     * 检查目录是否已收藏
     * @param directoryPath 要检查的目录路径
     * @return 如果已收藏返回true，否则返回false
     */
    public boolean isDirectoryFavorited(String directoryPath) {
        List<FavoriteDirectory> favoriteList = getAllFavorites();
        for (FavoriteDirectory favDir : favoriteList) {
            if (favDir.getDirectoryPath().equals(directoryPath)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 清空所有收藏目录
     */
    public void clearAllFavorites() {
        preferences.edit().remove(KEY_FAVORITE_LIST).apply();
    }

    /**
     * 保存收藏目录列表到SharedPreferences
     */
    private void saveFavoriteList(List<FavoriteDirectory> favoriteList) {
        String favoritesJson = gson.toJson(favoriteList);
        preferences.edit().putString(KEY_FAVORITE_LIST, favoritesJson).apply();
    }
}